<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-22
 * Time: 22:01
 */

namespace app\api\service\withdrawal;


use app\api\model\Assistant;
use app\api\model\CityPartner;
use app\api\model\CountyPartner;
use app\api\model\Member;
use app\api\model\ProvincePartner;
use app\api\model\User;
use app\api\model\UserInvitationMoney;
use app\api\model\Withdrawal;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class WithdrawalSer extends Token
{
    /**
     * 1.根据类型判断当前的信息
     * 2.查询正在提现的金额
     * 3.当前金额和所有金额进行对比
     * 4.大于可以提现，小于进行提示
     */

    // 用户ID
    private $uid;

    // 创建的信息
    private $postArr;

    // 查询信息
    private $info;

    function __construct($postArr)
    {
        $this->postArr = $postArr;
        $this->uid = self::getCurrentUid();
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeData();
    }

    // 判断数据
    private function judgeData()
    {
        if($this->postArr['type'] == 0) {
            $this->info = User::get($this->uid);
            if($this->info['status'] < 2) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '您还不是会员'
                    ]
                ]);
            }
            $member = Member::where('user_id',$this->info['id'])->find();
            if($member['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '会员信息正在审核中'
                    ]
                ]);
            }elseif($member['status'] == 2) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '会员审核未通过'
                    ]
                ]);
            }elseif($member['status'] == 3) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '会员已进入黑名单'
                    ]
                ]);
            }
            if($this->info['invite_money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->uid)
                ->where('status',1)
                ->where('type',$this->postArr['type'])
                ->sum('money');
            if($this->info['invite_money'] < round($money+$this->postArr['money'],2)) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $this->postArr['province'] = $member['province'];
            $this->postArr['city'] = $member['city'];
            $this->postArr['county'] = $member['county'];
        } elseif($this->postArr['type'] == 1) {
            $this->info = ProvincePartner::where('user_id',$this->uid)->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '您还不是省级合伙人'
                    ]
                ]);
            }
            if($this->info['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '省级合伙人还未审核通过'
                    ]
                ]);
            }
            if($this->info['status'] == 3) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '省级合伙人审核未通过'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('status',1)
                ->where('type',$this->postArr['type'])
                ->sum('money');
            if($this->info['money'] < round($money+$this->postArr['money'],2)) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $this->postArr['province'] = $this->info['province'];
            $this->postArr['city'] = '';
            $this->postArr['county'] = '';
        } elseif($this->postArr['type'] == 2) {
            $this->info = CityPartner::where('user_id',$this->uid)->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '您还不是市级合伙人'
                    ]
                ]);
            }
            if($this->info['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '市级合伙人还未审核通过'
                    ]
                ]);
            }
            if($this->info['status'] == 3) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '市级合伙人审核未通过'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('type',$this->postArr['type'])
                ->where('status',1)
                ->sum('money');
            if($this->info['money'] < round($money+$this->postArr['money'],2)) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $this->postArr['province'] = $this->info['province'];
            $this->postArr['city'] = $this->info['city'];
            $this->postArr['county'] = '';
        } elseif($this->postArr['type'] == 3) {
            $this->info = CountyPartner::where('user_id',$this->uid)
                ->where('status',2)
                ->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '您还不是区/县合伙人'
                    ]
                ]);
            }
            if($this->info['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '区/县合伙人还未审核通过'
                    ]
                ]);
            }
            if($this->info['status'] == 3) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '区/县合伙人审核未通过'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('type',$this->postArr['type'])
                ->where('status',1)
                ->sum('money');
            if($this->info['money'] < round($money+$this->postArr['money'],2)) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
        } elseif($this->postArr['type'] == 4) {
            $this->info = Assistant::where('user_id',$this->uid)->find();
            if(!$this->info) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '您还不是助理'
                    ]
                ]);
            }
            if($this->info['money'] <= 0) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
            $this->postArr['province'] = '';
            $this->postArr['city'] = '';
            $this->postArr['county'] = '';
            $money = Withdrawal::where('pid',$this->info['id'])
                ->where('type',$this->postArr['type'])
                ->where('status',1)
                ->sum('money');
            if($this->info['money'] < round($money+$this->postArr['money'],2)) {
                throw new ParameterException([
                    'msg' => [
                        'money' => '余额不足'
                    ]
                ]);
            }
        }
        return $this->createData();
    }

    private function createData()
    {
        $this->postArr['pid'] = $this->info['id'];
        Withdrawal::create($this->postArr);
        return writeJson(201, [], '申请成功！');
    }
}